package com.kms.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kms.derby.Connections;
import com.kms.model.MSyspara;
import com.kms.model.SystemParameterBean;
import com.kms.service.SystemparameterService;

public class TBSMonitorServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	SystemparameterService sysservice ;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		String id = request.getParameter("id");
		String ur = "";
		MSyspara mssys=new MSyspara();
		if(id !=null && !"null".equals(id)){
			mssys = sysservice.selectmSyspara(Integer.valueOf(id));
			if(mssys!=null){
				ur = mssys.getMonitorlogdir();
			}
		}
		
		
		List<SystemParameterBean> listsysbean = getListSysSetting(request);
		for(SystemParameterBean sb:listsysbean){
			if(sb.getName().contains("TBSMonitorServerURL")){
				ur = sb.getValue().trim();
			}
		}
		String status = "";
		String st = request.getParameter("status1");
		if("start".equals(st)){
			try {
				URL url = new URL(ur);
				BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));  
				String s = "";  
				StringBuffer sb = new StringBuffer("");  
				while ((s = br.readLine()) != null) {  
				    sb.append(s + "/r/n");  
				}  
				br.close();
				status = "alive";
			} catch (Exception e) {
				status = "error";
			} 
		}else if("stop".equals(st)){
			status = "stop";
		}
		
		request.setAttribute("status", status);
		request.setAttribute("url", ur);
		request.setAttribute("id", id);
		request.setAttribute("mssys", mssys);
		request.setAttribute("listmgroup",  sysservice.selectAllMGroup());
		request.getRequestDispatcher("tbsmonitor.jsp").forward(request,response);
		return;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		doGet(request, response);
	}
	
	public List<SystemParameterBean> getListSysSetting(HttpServletRequest request){
		List<SystemParameterBean> listsysbean = new ArrayList<SystemParameterBean>();
		try {
			Connection conn = Connections.getConnection(request);
			String SQL = "SELECT * FROM SYSSETTING";
			PreparedStatement stmt = conn.prepareStatement(SQL);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				SystemParameterBean sysb = new SystemParameterBean();
				sysb.setId(rs.getInt("Id"));
				sysb.setName(rs.getString("Name"));
				sysb.setType(rs.getString("Type"));
				sysb.setValue(rs.getString("Value"));
				sysb.setEnabled(rs.getString("Enabled"));
				sysb.setDescription(rs.getString("Description"));
				listsysbean.add(sysb);
			}
			
			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listsysbean;
	}
}
